<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="">
          <el-input
            style="min-width: 230px"
            v-model="listQuery.name"
            placeholder="输入内容"
            clearable
            size="small"
            @keyup.enter.native="handleFilter"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click.native="handleFilter"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增{{ this.title }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleBatchDelete"
            >批量删除</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="datas"
        border
        v-loading="listLoading"
        fit
        highlight-current-row
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          v-for="column in tableColumns"
          :key="column.key"
          :label="column.title"
          :prop="column.key"
          align="left"
          :width="column.width ? column.width : ''"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
       <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getHouseList"
      />
    </el-card>
    <!-- 对话框表单 -->
    <el-dialog :visible.sync="dialogVisible" :title="getTitle" width="600px">
      <el-form
        :model="form"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
        label-position="right"
        size="small"
        style="width: 95%; padding: 0 30px"
        status-icon
      >
        <el-form-item label="仓库名称" prop="name">
          <el-input
            type="string"
            v-model="form.name"
            placeholder="仓库名称"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input
            v-model="form.address"
            placeholder="仓库地址"
            clearable
            size="small"
          />
        </el-form-item>

        <el-form-item label="仓库地址" prop="flag">
          <el-select v-model="form.flag" placeholder="">
            <el-option v-for="item in flags" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
            
        </el-form-item>
        
      </el-form>
      <div slot="footer">
        <el-button type="danger" size="small" @click="dialogVisible = false"
          >取消</el-button
        >
        <el-button type="primary" size="small" @click="handleSave(false)"
          >保存</el-button
        >
        <el-button type="primary" size="small" @click="handleSave(true)"
          >保存并新增</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { getHouses, addHouse, updateHouse, delHouse, delHouses } from "@/api/base";

const defaultForm = {
  id: null,
  name: "",
  address: "",
  flag: 0,
};

export default {
  name: "House",
  components:{
    Pagination
  },
  data() {
    return {
      form: Object.assign({}, defaultForm),
      formRules: {
        name: [
          {
            type: "string",
            required: true,
            message: "请输入仓库名称",
            trigger: "blur",
          },
        ],
        address: [
          {
            type: "string",
            required: true,
            message: "请输入仓库名称",
            trigger: "blur",
          },
        ],
      },

      tableColumns: [
        {
          key: "id",
          title: "仓库编码",
        },
        {
          key: "name",
          title: "仓库名称",
        },
        {
          key: "address",
          title: "仓库地址",
        },
        {
          key: "flag_name",
          title: "默认仓库",
        },
      ],

      title: "仓库",
      dialogVisible: false,
      dialogType: "new",
      datas: [],
      selections: [],

      listLoading: true,
      total: 0,
      listQuery: {
        name: "",
        page: 1,
        page_size: 15,
      },

      flags: [
        {
          value: 0,
          label: "否",
        },
        {
          value: 1,
          label: "是",
        },

      ],
    };
  },
  computed: {
    getTitle() {
      return this.dialogType == "new"
        ? `新增${this.title}`
        : `编辑${this.title}`;
    },
  },

  created() {
    this.getHouseList();
  },
  methods: {
    getHouseList() {
      this.listLoading = true;
      getHouses(this.listQuery)
        .then((res) => {
          this.datas = res.results;
          this.total = res.count;
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("获取数据失败!" + error);
          this.listLoading = false;
        });
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1;
      this.getHouseList();
    },
    // 增加
    handleAdd() {
      this.form = Object.assign({}, defaultForm);

      this.dialogType = "new";
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
    // 修改
    handleEdit(scope) {
      this.form = scope.row;
      this.dialogType = "edit";

      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
    // 删除
    handleDelete({ $index, row }) {
      this.$confirm("确定要删除该记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delHouse(row.id);
          this.getHouseList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          this.$message({
            type: "fail",
            message: "删除失败!" + err,
          });
        });
    },
    handleSelectionChange(val) {
      this.selections = val.map((item) => item.id);
    },
    //批量删除
    handleBatchDelete() {
      if (this.selections.length == 0) {
        this.$message.warning("请选择要删除记录");
        return;
      }
      this.$confirm("确定要删除选中记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const sel_ids = {
            select_ids: this.selections,
          };
          const res = await delHouses(sel_ids);
          this.getHouseList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          this.$message({
            type: "success",
            message: "删除失败!" + err,
          });
        });
    },

    // 保存操作
    async handleSave(flag) {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return;

        const isEdit = this.dialogType === "edit";

        let res = null;
        if (isEdit) {
          res = await updateHouse(this.form);
        } else {
          res = await addHouse(this.form);
        }

        this.getHouseList();
        
        const { name,address } = res;

        if (flag) {
          this.form = Object.assign({}, defaultForm);
          this.dialogType = "new";
        } else {
          this.dialogVisible = false;
        }

        this.$notify({
          title: "Success",
          dangerouslyUseHTMLString: true,
          message: `
              <div>仓库名称: ${name}</div>
              <div>仓库地址: ${address}</div>
            `,
          type: "success",
        });
      });
    },
  },
};
</script>

<style lang='scss' scoped>
</style>